package lee.vioson.remotedata.repositories

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import lee.vioson.remotedata.ApiServices
import lee.vioson.remotedata.bean.FeedbackDetail
import lee.vioson.remotedata.bean.requestbody.FeedbackBody
import lee.vioson.remotedata.bean.requestbody.FeedbackDetailRequestBody
import lee.vioson.remotedata.bean.requestbody.UniqueRequestBody

/**
 *Author:viosonlee
 *Date:2023/6/24
 *DESCRIPTION:
 */
class FeedbackRepositoryImpl(private val apiServices: ApiServices) : BaseRepository(),
    FeedbackRepository {
    override fun commitFeedback(feedbackBody: FeedbackBody) = flow {
        emit(handleNotEmptyResponse(apiServices.saveFeedback(feedbackBody))["msgId"])
    }

    override suspend fun fetchFeedbackList(uniqueRequestBody: UniqueRequestBody) =
        handleNotEmptyResponse(apiServices.fetchFeedbackList(uniqueRequestBody)).list
            ?: emptyList()

    override fun fetchFeedbackDetail(msgId: String) = flow {
        emit(
            handleNotEmptyResponse(
                apiServices.fetchFeedbackDetail(
                    FeedbackDetailRequestBody(msgId)
                )
            ).list ?: emptyList()
        )
    }

}